using UnityEngine;
using UnityEngine.Rendering;

#if UNITY_EDITOR
using UnityEditor;
#endif

public class IrradianceVolumeData : ScriptableObject
{
    public Vector3[] Postition;
    public Vector3[] L0;

#if UNITY_EDITOR
    public static void CompressAndSave()
    {
        var probes = LightmapSettings.lightProbes;
        if (probes == null || probes.count == 0)
        {
            Debug.LogError("场景未烘培，没有light probes");
            return;
        }
        
        Vector3[] positions = probes.positions;
        SphericalHarmonicsL2[] shArray = probes.bakedProbes;
        
        string dir = "Assets/IrradianceVolume/Data";
        if (!AssetDatabase.IsValidFolder(dir))
            AssetDatabase.CreateFolder("Assets/IrradianceVolume", "Data");

        var asset = ScriptableObject.CreateInstance<IrradianceVolumeData>();
        asset.Postition = positions;
        asset.L0 = new Vector3[probes.bakedProbes.Length];

        for (int i = 0; i < shArray.Length; i++)
        {
            asset.L0[i] = new Vector3(shArray[i].EvaluateL0().r,
                shArray[i].EvaluateL0().g,
                shArray[i].EvaluateL0().b);
        }
        
        string path = dir + "/IrradianceVolumeData.asset";
        AssetDatabase.CreateAsset(asset, path);
        AssetDatabase.SaveAssets();
        
        Debug.Log($"Compressed data saved to {path}");
    }
#endif
}
